@description('The location for the resource(s) to be deployed.')
param location string = resourceGroup().location

param principalId string

param principalType string

resource openai 'Microsoft.CognitiveServices/accounts@2022-12-01' = {
    name: toLower(take('openai${uniqueString(resourceGroup().id)}', 24))
    location: location
    kind: 'OpenAI'
    properties: {
        customSubDomainName: toLower(take(concat('openai', uniqueString(resourceGroup().id)), 24))
        publicNetworkAccess: 'Enabled'
        disableLocalAuth: true
    }
    sku: {
        name: 'S0'
    }
    tags: {
        'aspire-resource-name': 'openai'
    }
}

resource openai_CognitiveServicesOpenAIContributor 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
    name: guid(openai.id, principalId, subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'a001fd3d-188f-4b5d-821b-7da978bf7442'))
    properties: {
        principalId: principalId
        roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'a001fd3d-188f-4b5d-821b-7da978bf7442')
        principalType: principalType
    }
    scope: openai
}

resource gpt-4o 'Microsoft.CognitiveServices/accounts/deployments@2022-12-01' = {
    name: 'gpt-4o'
    properties: {
        model: {
            format: 'OpenAI'
            name: 'gpt-4o'
            version: '2024-05-13'
        }
    }
    sku: {
        name: 'Standard'
        capacity: 8
    }
    parent: openai
}

output connectionString string = 'Endpoint=${openai.properties.endpoint}'